<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../base.jsp" %>
<script type="text/javascript">
$(function() {
	init();
});
function init(){
	$.ajax({
		type:"POST",
		url: path+'collect/getAllNodes',
		success: function(result){
			$("#nodeLi").html("");
			var index = 1;
			for(var item in result.result){
				if(index==1){
					showNodeInfo(item);
				}
				var appendStr ="<li id="+index+" onclick=showNodeInfo(\""+item+"\")><a href='javascript:;'><i class='fa fa-server'></i>"+item+"</a></li>";
				$("#nodeLi").append(appendStr);
				index = parseInt(index)+1;
			}
		}
	}); 
}
function showNodeInfo(ip){
	$("#beatH3").html(ip);
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'collect/getAllNodes',
		success: function(result){
			$("#beatInfo").html("");
			$("#addServiceTbody").html("");
			for(var i=0;i<result.result[ip].services.length;i++){
				for(var item in result.result[ip].services[i]){
					if(result.result[ip].services[i][item].state==1){
						var appendStr = "<tr><td><a href='javascript:;' onclick='toNode(\""+item+"\")'>"+item+"</a></td>";
						appendStr+="<td><b>1</b></td>";
						appendStr+="<td><i class='fa fa-check-circle text-green'></i></td>";	
						appendStr = showStatus(appendStr, item,ip);
						appendStr+="</tr>";
						$("#beatInfo").append(appendStr);
					}else{
						var appendStr = "<tr><td><a href='javascript:;'>"+item+"</a></td>";
						appendStr+="<td><b>1</b></td>";
						appendStr+="<td><i class='fa fa-warning text-yellow'></i></td>";	
						appendStr+="<td><a href='#' title='添加' onclick='updateState(\""+item+"\",\""+ip+"\",1)'><i class='fa fa-plus text-blue'></i>添加</a></td>";
						appendStr+="</tr>";
						$("#addServiceTbody").append(appendStr);
					}
				}
			}
		}
	}); 
}

function updateState(item,ip,state){
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'collect/updateState',
		data:{serviceName:item,state:state,ip:ip},
		success: function(result){
			if(result){
				alert("操作成功");
				showNodeInfo(ip);
			}
		}
	});
}

function showStatus(appendStr,item,ip){
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'collect/getBeatStatus',
		data:{beatName:item,ip:ip},
		success: function(result){
			if(result){
				if(result.result[item].status=="running"){
					appendStr+="<td><a href='#' title='重启' onclick='reStartNode(\""+item+"\",\""+ip+"\")'><i class='fa fa-refresh text-green'></i>重启</a><a href='#' title='停止' onclick='stopNode(\""+item+"\",\""+ip+"\")'><i class='fa fa-stop text-yellow'></i>停止</a></td>";
				}else{
					appendStr+="<td><a href='#' title='启动' onclick='startNode(\""+item+"\",\""+ip+"\")'><i class='fa fa-play text-blue'></i>启动</a><a href='#' title='删除' onclick='updateState(\""+item+"\",\""+ip+"\",0)'><i class='fa fa-close text-red'></i>删除</a></td>";	
				}
			}
		}
	});
	return appendStr;
}

function startNode(item,ip){
	if(confirm('确实要启动吗?')){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'collect/startBeatService',
			data:{beatName:item,ip:ip},
			success: function(result){
					if(result){
						alert("启动成功");
						showNodeInfo(ip);
					}
			}
		});
	}
}

function stopNode(item,ip){
	if(confirm('确实要停止吗?')){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'collect/stopBeatService',
			data:{beatName:item,ip:ip},
			success: function(result){
					if(result){
						alert("停止成功");
						showNodeInfo(ip);
					}
			}
		});
	}
}

function reStartNode(item,ip){
	if(confirm('确实要重启吗?')){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'collect/stopBeatService',
			data:{beatName:item,ip:ip},
			success: function(result){
					if(result){
						$.ajax({
							cache:false,
							async:false,
							type:"POST",
							url: path+'collect/startBeatService',
							data:{beatName:item,ip:ip},
							success: function(result){
									if(result){
										alert("重新启动成功");
										showNodeInfo(ip);
									}
							}
						});
					}
			}
		});
	}
}
</script>
<style>
	.content-wrapper{margin-left: 0px}
</style>
<body class="hold-transition skin-blue sidebar-mini">
	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>采集配置<small>节点管理</small></h1>
				<ol class="breadcrumb">
					<li><a href="javascript:;"><i class="fa fa-dashboard"></i> 首页</a></li>
					<li>主机</li>
					<li class="active">主机配置</li>
				</ol>
			</section>
			<!-- Main content -->
			<section class="content">
				<div class="row">
        			<div class="col-md-3">
        				<div class="box box-solid">
				          <div class="box-header with-border">
				            <h3 class="box-title">概览</h3>
				            <div class="box-tools">
				              <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
				              </button>
				            </div>
				          </div>
				          <div class="box-body no-padding">
				            <ul class="nav nav-pills nav-stacked" id="nodeLi">
				              
				            </ul>
				          </div>
				          <!-- /.box-body -->
				        </div>
        			</div>
        			<div class="col-md-9" id="local-list">
       					<jsp:include page="beat_list.jsp"></jsp:include>
        			</div>
        		</div>
			</section>
		</div>
	</div>
	<script type="text/javascript">
	$(function () {
		$('.nav-stacked li').on('click',function(){
			$('.nav-stacked li').removeClass('active');
			$(this).addClass('active');
			var id = $(this).attr('id');
			linkByDom('local-list','${url }collect/getBeatList');
		});
	});
	
	</script>
</body>